package com.yanqu.road.server.timer.xingyoushanshui;

import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouActivity;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouShanShuiMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

public class XingYouShanShuiTask  extends GeneralTask {

    private static long lastRestTime = DateHelper.getTodayZeroTimeStamp();

    @Override
    public void doRun() {

        try {
            for (Map.Entry<Integer, Map<Integer, CrossXingYouActivity>> entry : CrossXingYouShanShuiMgr.getActivityMap().entrySet()) {
                for (Map.Entry<Integer, CrossXingYouActivity> activityEntry : entry.getValue().entrySet()) {
                    CrossXingYouActivity value = activityEntry.getValue();
                    long now = System.currentTimeMillis() / 1000;
                    if (now >= value.getConfig().getActivityInfo().getBeginShowTime() && now <= value.getConfig().getActivityInfo().getEndTime()) {
                        value.notifyUploadBeautyData();
                    }
                }
            }
        }catch (Exception e){
            getLOGGER().error("error : ", e);
        }

        try {
            for (Map.Entry<Integer, Map<Integer, CrossXingYouActivity>> entry : CrossXingYouShanShuiMgr.getActivityMap().entrySet()) {
                for (Map.Entry<Integer, CrossXingYouActivity> activityEntry : entry.getValue().entrySet()) {
                    CrossXingYouActivity value = activityEntry.getValue();
                    CommonTeamActivity activity = CommonTeamMgr.getActivity(entry.getKey(), activityEntry.getKey());
                    if (activity == null) {
                        continue;
                    }
                    //组队期前20分钟，初始地图数据（挑战格、衙门、建筑）
                    long teamEndTime = (activity.getConfig().getCreateTeamTime() - 20) * DateHelper.MINUTE_SECONDS;
                    long now = System.currentTimeMillis() / 1000;
                    if (now >= (value.getConfig().getActivityInfo().getBeginTime() + teamEndTime) && now <= value.getConfig().getActivityInfo().getEndTime()) {
                        value.initBeforeActivity();
                    }
                }
            }
        }catch (Exception e){
            getLOGGER().error("error : ", e);
        }

        long todayZeroTimeStamp = DateHelper.getTodayZeroTimeStamp();
        if(todayZeroTimeStamp != lastRestTime){
            lastRestTime = todayZeroTimeStamp;

            for (Map.Entry<Integer, Map<Integer, CrossXingYouActivity>> activityEntry : CrossXingYouShanShuiMgr.getActivityMap().entrySet()) {
                for (Map.Entry<Integer, CrossXingYouActivity> entry : activityEntry.getValue().entrySet()) {
                    try {
                        CrossXingYouActivity activity = entry.getValue();
                        if (ActivityMgr.activityInTime(activity.getConfig().getActivityInfo())) {
                            activity.dayReset();
                        }
                    } catch (Exception e) {
                        getLOGGER().error("error : ", e);
                    }
                }
            }
        }

        int currentSecond = DateHelper.getCurrentSecond();
        for (Map.Entry<Integer, Map<Integer, CrossXingYouActivity>> activityEntry : CrossXingYouShanShuiMgr.getActivityMap().entrySet()) {
            for (Map.Entry<Integer, CrossXingYouActivity> entry : activityEntry.getValue().entrySet()) {
                try {
                    CrossXingYouActivity activity = entry.getValue();
                    if (currentSecond > activity.getConfig().getActivityInfo().getEndTime()) {
                        activity.sendHelpReward();
                    }
                } catch (Exception e) {
                    getLOGGER().error("error : ", e);
                }
            }
        }
    }
}
